﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using ACH.Properties;

namespace ACH
{
    public static class Helper
    {
        public static void CreateNew(BindingSource bindingSource1)
        {
            (bindingSource1[bindingSource1.Position] as DataRowView)["ID"] = Guid.NewGuid();
            (bindingSource1[bindingSource1.Position] as DataRowView)["CreateDate"] = DateTime.Now;
            (bindingSource1[bindingSource1.Position] as DataRowView)["UpdateDate"] = DateTime.Now;
            (bindingSource1[bindingSource1.Position] as DataRowView)["UpdateUser"] = "????";
            (bindingSource1[bindingSource1.Position] as DataRowView)["CreateUser"] = "????";
        }

        public static DataTable ExecuteSql(string sqlSelect)
        {
            return ExecuteSql(sqlSelect, null);
        }

        public static DataTable ExecuteSql(string sqlSelect, SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(Settings.Default.ACHConnectionString))
            {
                SqlCommand cmd = new SqlCommand(sqlSelect, conn);
                if (parameters != null)
                    cmd.Parameters.AddRange(parameters);

                try
                {
                    conn.Open();
                    DataTable dt = new DataTable();
                    dt.Load(cmd.ExecuteReader());
                    return dt;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return null;
                }
            }
        }
    }
}
